package springboot.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springboot.Tools.msgTool;
import springboot.doMain.Msg;
import springboot.doMain.Staff;
import springboot.doMain.WxGroup;
import springboot.http.WXWorkApi;
import springboot.service.impl.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class test_controller {
	
	final
	MsgServiceImpl msgService;
	final
	EngineerServiceImpl engineerService;
	final
	TbProblemStatusServiceImpl tbProblemStatusService;
	final
	StaffServiceImpl staffService;
	
	public test_controller(MsgServiceImpl msgService, EngineerServiceImpl engineerService, TbProblemStatusServiceImpl tbProblemStatusService, StaffServiceImpl staffService, WXGroupServiceImpl wxGroupService) {
		this.msgService = msgService;
		this.engineerService = engineerService;
		this.tbProblemStatusService = tbProblemStatusService;
		this.staffService = staffService;
		this.wxGroupService = wxGroupService;
	}
	
	@GetMapping("/aa")
	String string() {
		
		return "index";
	}
	
	
	@ResponseBody
	@GetMapping("msg_engineer_need_reply")
	public List<Msg> msg_engineer_need_reply(String name) {
		Msg msg = new Msg();
		msg.setEngineer(name);
		return msgService.msg_engineer_need_reply(msg);
	}
	
	@ResponseBody
	@GetMapping("msg_engineer_need_resolve")
	public List<Msg> msg_engineer_need_resolve(String name) {
		Msg msg = new Msg();
		msg.setEngineer("郑治");
		
		msg.setEngineer(name);
		
		return msgService.msg_engineer_need_resolve(msg);
	}
	
	@ResponseBody
	@GetMapping("msg_all_need_reply")
	public List<Msg> msg_all_need_reply() {
		
		return msgService.msg_all_need_reply();
	}
	
	@ResponseBody
	@GetMapping("msg_all_need_resolve")
	public List<Msg> msg_all_need_resolve() {
//        msgTool.jsonToMsg(data)
		return msgService.msg_all_need_resolve();
	}
	
	@ResponseBody
	@GetMapping("msg_all_had_reply")
	public List<Msg> msg_all_had_reply() {
		return msgService.msg_all_had_reply();
	}
	
	@ResponseBody
	@GetMapping("msg_all_had_resolve")
	public List<Msg> msg_all_had_resolve() {
		return msgService.msg_all_had_resolve();
	}
	
	
	@ResponseBody
	@GetMapping("msg_search")
	public List<Msg> msg_search(String data) {
		
		return msgService.msg_search(msgTool.jsonToMsg(data));
//        return null;
	}
	
	
	@ResponseBody
	@GetMapping("msg_byGroupId")
	public List<Msg> msg_byGroupId(String data) {
		if (data == null) {
			return null;
		}
		
		JSONObject jsonData = JSON.parseObject(data);
		Msg msg = new Msg();
//        {"type":"message","status":0,"engineer_id":"刘嘉诚"}
		msg.setEngineer(jsonData.getString("engineer_id"));
		msg.setType(jsonData.getString("type"));
		msg.setStatus(Integer.valueOf(jsonData.getString("status")));
		msg.setCpId(jsonData.getString("cp_id"));
		msg.setTime(jsonData.getInteger("time"));
		msg.setEndTime(jsonData.getInteger("endTime"));
		return msgService.msg_byGroupId(msg);
	}
	
	@ResponseBody
	@GetMapping("sel_all_engineer")
	public Map<String, Object> sel_all_engineer() {
		Subject currentUser = SecurityUtils.getSubject();
		Staff staff = (Staff) currentUser.getPrincipal(); // 获取当前登录用户
		if (staff == null) {
			return null;
		}
		staff.setPassword(null);
		Map<String, Object> objectMap = new HashMap<>();
		objectMap.put("engineers", staffService.sel_all_engineer());
		objectMap.put("currentUser", staff);
		return objectMap;
	}
	
	@ResponseBody
	@GetMapping("all_group_message")
	public List<Msg> all_group_message(String data) {
		JSONObject jsonData = JSON.parseObject(data);
		Msg msg = new Msg();
//        {"type":"message","status":0,"engineer_id":"刘嘉诚"}
		msg.setEngineer(jsonData.getString("engineer_id"));
		msg.setType(jsonData.getString("type"));
		msg.setStatus(Integer.valueOf(jsonData.getString("status")));
		msg.setTime(jsonData.getInteger("time"));
		msg.setEndTime(jsonData.getInteger("endTime"));
		
		return msgService.all_group_message(msg);
	}
	
	
	@ResponseBody
	@GetMapping("update_msg_status")
	public int update_msg_status(String data) {
		Staff staff = (Staff) SecurityUtils.getSubject().getPrincipal(); // 获取当前登录用户
		if (staff == null) {
			return 0;
		}
		JSONObject jsonData = JSON.parseObject(data);
		Msg msg = new Msg();
		/*消息*/
		/*{"engineer":"郑治","hq":"21317","type":"message","status":0}*/
		/*问题*/
		/*{"solution":"aaa","engineer":"郑治","hq":"4693","type":"problem","status":1,"processorDepartment":0}*/
		msg.setProcessor(staff.getName());
		msg.setType(jsonData.getString("type"));
		msg.setStatus(jsonData.getInteger("status"));
		msg.setHq(jsonData.getInteger("hq"));
		/*如果是问题，则获取解决类型以及解决方案*/
		if (!msg.getType().equals("message")) {
			msg.setProcessorDepartment(jsonData.getInteger("processorDepartment"));
			msg.setSolution(jsonData.getString("solution"));
		}
		msg.setEndTime(System.currentTimeMillis());
		/*获取当前时间时间戳13位*/
		return msgService.update_msg_status(msg);
	}
	
	@ResponseBody
	@GetMapping("ask_all_message")
	public List<Msg> ask_all_message(String data) {
		/* {"type":"message","status":0,"engineer_id":"all_engineer"}*/
		if (data == null) {
			return null;
		}
		JSONObject jsonObject = JSON.parseObject(data);
		Msg msg = new Msg();
		msg.setType(jsonObject.getString("type"));
		msg.setStatus(Integer.valueOf(jsonObject.getString("status")));
		msg.setEngineer(jsonObject.getString("engineer_id"));
		msg.setTime(jsonObject.getInteger("time"));
		msg.setEndTime(jsonObject.getInteger("endTime"));
		List<Msg> msgList = msgService.ask_all_message(msg);
		msgList.forEach(msg1 -> {
			if (msg1.getCpId().equals(msg1.getCpName())) {
				new Thread(this::updateGroupName).start();
			}
		});
		return msgList;
	}
	
	final
	WXGroupServiceImpl wxGroupService;
	
	void updateGroupName() {
		String token = WXWorkApi.getToken();
		List<Map<Object, Object>> notInsertWxGroup = msgService.getNotInsertWxGroup();
		notInsertWxGroup.forEach(objectObjectMap -> {
			Msg msg = new Msg();
			msg.setCpId(objectObjectMap.get("chat_id") + "");
			String groupInfo = WXWorkApi.getGroupInfo(msg.getCpId(), token);
			WxGroup instance = WxGroup.getInstance(groupInfo);
//			System.out.println("instance = " + instance);
			if (instance != null) {
				wxGroupService.insert(instance);
				msg.setCpName(instance.getName());
				msg.setCpId(instance.getChatId());
//				System.out.println("instance = " + instance);
				int i1 = msgService.updateGroupName(msg);
				int i = msgService.updateAllMsgGroupName(msg);
				System.out.println("i = " + i);
				System.out.println("i1 = " + i1);
			} else {
				WxGroup wxGroup = new WxGroup();
				wxGroup.setChatId(msg.getCpId());
				System.out.println("wxGroup = " + wxGroup);
				WxGroup wxGroupByChatId = wxGroupService.selWxGroupByChatId(wxGroup);
				Msg msg1 = new Msg();
				if (wxGroupByChatId == null) {
					System.out.println(msg.getCpId());
				} else {
					msg1.setCpId(wxGroupByChatId.getChatId());
					msg1.setCpName(wxGroupByChatId.getName());
					int i1 = msgService.updateGroupName(msg1);
					int i = msgService.updateAllMsgGroupName(msg1);
					System.out.println("j = " + i);
					System.out.println("j1 = " + i1);
				}
			}
		});
	}
	
	//    同时查询两个
	@ResponseBody
	@GetMapping("selMsgByHq")
	public Map<String, Object> selMsgByHq(Integer hq) {
		Map<String, Object> objectHashMap = new HashMap<>();
		objectHashMap.put("problemStatusByHq", tbProblemStatusService.problemStatusByHq(hq));
		objectHashMap.put("selMsgByHq", msgService.selMsgByHq(hq));
		
		return objectHashMap;
	}
	
	
}
